package com.autumn.platform.dao.schema.script.extend;

import java.util.List;

import org.apache.ibatis.parsing.XNode;
import org.apache.ibatis.scripting.xmltags.DynamicContext;
import org.apache.ibatis.scripting.xmltags.MixedSqlNode;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.session.Configuration;

import com.autumn.platform.dao.DaoUtils;
import com.autumn.platform.dao.schema.script.ScriptHandlerSupport;

public class DbScriptHandler extends ScriptHandlerSupport {

    @Override
    public void handleScriptNode(Configuration configuration, XNode node, List<SqlNode> targetContents) {
        if (DaoUtils.isMatchDatabase(configuration.getDatabaseId(), node.getStringAttribute("type"))) {
            List<SqlNode> contents = parseDynamicTags(configuration, node);
            final MixedSqlNode mixedSqlNode = new MixedSqlNode(contents);
            targetContents.add(new SqlNode() {
                @Override
                public boolean apply(DynamicContext context) {
                    mixedSqlNode.apply(context);
                    return true;
                }
            });
        }
    }
}
